//
//  SXMainViewController.swift
//  sina
//
//  Created by shengxinwei on 16/5/19.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit

class SXMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        let mainTabBar = SXMainTabBar()
        //        self.tabBar = mainTabBar  赋值 只读属性不能够直接赋值 但是 可以通过 KVC 间接赋值
        setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.pushBtn.addTarget(self, action: #selector(SXMainViewController.pushBtnDidClick), forControlEvents: .TouchUpInside)
        
        
        
        
        
    }
    
    //为了让外界不能够访问按钮的监听方法 需要添加private 私有
    //一旦添加private 这个时候这个 就不具备动态派发的特性
    //需要添加 @objc 告诉系统这个方法 是基于OC消息动态派发的
    @objc private func pushBtnDidClick() {
        //print("被点击,,供未来使用")
        let composeView = SXComposeView()
        composeView.show(targetVc: self)
//        let window = UIApplication.sharedApplication().keyWindow
//        window?.addSubview(composeView)
    }
    
    
    private func addChildViewControllers() {
        addChildViewController(SXHomeViewController(), title: "首页", imageName: "tabbar_home",tag: 0)
        addChildViewController(SXMessageViewController(), title: "消息", imageName: "tabbar_message_center",tag: 1)
        addChildViewController(SXDiscoverViewController(), title: "发现", imageName: "tabbar_discover",tag: 2)
        addChildViewController(SXProfileViewController(), title: "我的", imageName: "tabbar_profile",tag: 3)
    }
    
    //调用此方法创建tabBar
    private func addChildViewController(vc: UIViewController,title: String,imageName: String,tag: Int) {
        vc.title = title
        //vc.tabBarItem.title = "首页"
        //设置tabBarItem 图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //设置tabBar选中字体属性状态,,NSForegroundColorAttributeName : 字体前景色颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        //设置tabBar选中时图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        vc.tabBarItem.tag = tag
        
        //设置
        let  nav = SXBaseNavController(rootViewController: vc)
        //添加子控器
        addChildViewController(nav)
    }
    
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        ///UITabBarSwappableImageView
        //print(item.tag)
        var index: Int = 0
        for vcSub in tabBar.subviews {
            if vcSub.isKindOfClass(NSClassFromString("UITabBarButton")!){
                if index == item.tag {
                    for subview in vcSub.subviews {
                        if subview.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                            subview.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            //给UITabBarSwappableImageView添加动画效果
                            UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                subview.transform = CGAffineTransformIdentity
                                }, completion: { (_) in
                                    
                            })
                        }
                    }
                }
                index += 1
            }
            
        }
    }
    
    
    
    
    
}
